package org.greenrobot.eventbus.annotationprocessor;


import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.VariableElement;

/**
 * Created by harryguo on 2018/7/22.
 */
public class SubscribeMethodElement {
    private ExecutableElement mExecutableElement;
    private Name mMethodName;

    public SubscribeMethodElement(Element element) throws IllegalArgumentException {
        if (element.getKind() != ElementKind.METHOD) {
            throw new IllegalArgumentException(
                    String.format("Only methods can be annotated with @%s", Subscribe.class.getSimpleName()));
        }
        mExecutableElement = (ExecutableElement) element;
        mMethodName = mExecutableElement.getSimpleName();
        List<? extends VariableElement> parameters = mExecutableElement.getParameters();

        if (parameters.size() != 1) {
            throw new IllegalArgumentException(
                    String.format("The method annotated with @%s must have only 1 parameter", Subscribe.class.getSimpleName()));
        }
    }
}
